package com.pms.web;


import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.pms.entity.Mind;
import com.pms.entity.Task;
import com.pms.entity.User;
import com.pms.entity.UserProject;
import com.pms.service.MindService;
import com.pms.service.TaskService;
import com.pms.util.Page;

public class MindServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		String method=request.getParameter("m");
		if(method==null||"".equals(method)){
			list(request,response);
		}else if("addmind".equals(method)){
			addMind(request,response);
		}
	}
	private void addMind(HttpServletRequest request,
			HttpServletResponse response) throws IOException, ServletException {
		HttpSession session=request.getSession();
		String desc=request.getParameter("desc");
		UserProject up=(UserProject)session.getAttribute("up");
		String userId=up.getUserId();
		String projectId=up.getProjectId();
		MindService mindService=new MindService();
		mindService.save(desc,userId, projectId);
		response.sendRedirect("mind.jspx");
		
	}
	private void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session=request.getSession();
		String p=request.getParameter("p");
		int pageNum=0;
		if(p==null||"".equals(p)){
			
			pageNum=1;
		}else{
			pageNum=Integer.parseInt(p);
			
		}
		
		UserProject up=(UserProject)session.getAttribute("up");
		String projectId=up.getProjectId();
		MindService mindService=new MindService();
		Page<Mind> mindList=mindService.findAll(projectId,pageNum);
		session.setAttribute("mindlist", mindList);
		User user = up.getUser();
		
		TaskService taskService = new TaskService();
		Page<Task> page = taskService.findAll(pageNum,user);
		request.setAttribute("taskpage", page);
		
		request.getRequestDispatcher("/WEB-INF/views/project.jsp").forward(request, response);
		
	}
}
